package com.always.electrical.energy.web.controller;

import com.always.electrical.energy.web.req.DeviceIdsReq;
import com.always.electrical.energy.web.req.DevicesControlMachineModeReq;
import com.always.electrical.energy.web.req.DevicesControlRunningStateReq;
import com.always.electrical.energy.web.req.DevicesPowerFactorReq;
import com.always.electrical.energy.web.res.WebBaseResponse;
import com.always.electrical.energy.web.service.DevicesService;
import com.always.electrical.energy.web.service.MockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

/**
 * SVG、AUC、FVR
 */
@RestController
@RequestMapping("/devices")
public class DevicesController {

    @Value("${web.timeOut}")
    private Long timeOut;

    @Autowired
    private DevicesService devicesService;

    @Autowired
    private MockService mockService;

    /**
     * 设备查询 状态 召测
     */
    @PostMapping("/control/get/state")
    public DeferredResult<WebBaseResponse> controlGetRunningState(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        devicesService.controlGetRunningState(result, req.getIds());
        return result;
    }

    /**
     * 设备查询 模式 召测
     */
    @PostMapping("/control/get/mode")
    public DeferredResult<WebBaseResponse> controlGetMachineMode(@RequestBody DeviceIdsReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        devicesService.controlGetMachineMode(result, req.getIds());
        return result;
    }

    /**
     * 设备控制 状态 设置
     */
    @PostMapping("/control/set/state")
    public DeferredResult<WebBaseResponse> ControlSetRunningState(@RequestBody DevicesControlRunningStateReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        devicesService.controlSetRunningState(result, req.getItems());
        return result;
    }

    /**
     * 设备控制 模式 设置
     */
    @PostMapping("/control/set/mode")
    public DeferredResult<WebBaseResponse> controlSetMachineMode(@RequestBody DevicesControlMachineModeReq req) {
        DeferredResult<WebBaseResponse> result = new DeferredResult<>(timeOut);
        result.onTimeout(() -> result.setResult(WebBaseResponse.errorConnectMqtt()));
        mockService.controlSetMachineMode(result, req.getItems());
        return result;
    }

    /**
     * 目标功率因数 查询
     */
    @PostMapping("/control/get/factor")
    public WebBaseResponse targetPowerFactorGet(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(devicesService.targetPowerFactorGet(req.getIds()));
    }

    /**
     * 目标功率因数 设置
     */
    @PostMapping("/control/set/factor")
    public WebBaseResponse targetPowerFactorSet(@RequestBody DevicesPowerFactorReq req) {
        return WebBaseResponse.success(mockService.targetPowerFactorSet(req.getItems()));
    }

    /**
     * 电容器投切状态 查询
     */
    @PostMapping("/capacitor/state")
    public WebBaseResponse capacitorState(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(devicesService.capacitorState(req.getIds()));
    }

    /**
     * 显示数据数据项
     */
    @PostMapping("/svgCurve")
    public WebBaseResponse svgCurveData(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(devicesService.svgCurveData(req.getIds()));
    }

    /**
     * 变压器 查询
     */
    @PostMapping("/transformer")
    public WebBaseResponse transformer(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(devicesService.transformer(req.getIds()));
    }

    /**
     * 融合终端 查询
     */
    @PostMapping("/convergedTerminals")
    public WebBaseResponse convergedTerminals(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(mockService.convergedTerminals(req.getIds()));
    }

    /**
     * 分支箱 查询
     */
    @PostMapping("/branchBoxes")
    public WebBaseResponse branchBoxes(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(devicesService.ltu(req.getIds()));
    }

    /**
     * 电线杆 查询
     */
    @PostMapping("/pole")
    public WebBaseResponse pole(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(mockService.pole(req.getIds()));
    }

    /**
     * 电表 查询
     */
    @PostMapping("/meter")
    public WebBaseResponse meter(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(mockService.meter(req.getIds()));
    }

    /**
     * 通电状态 查询
     */
    @PostMapping("/svgCurveRunningState")
    public WebBaseResponse svgCurveRunningState(@RequestBody DeviceIdsReq req) {
        return WebBaseResponse.success(devicesService.svgCurveRunningState(req.getIds()));
    }
}
